import amap from "@/common/amap-wx.js";
import { def } from "@vue/shared";

const utils = {
    /**
     * 获取定位
     * @return promise=> resolve =>{
        areaStr: "越秀区"
        cityStr: "广州市"
        deatilAddr: "广州市越秀区府前路1号"
        lat: 23.130061
        lon: 113.264499
        province: "广东省"
        townStr: "北京街道"
     * }
     */
    getLocation () {
        const GDkey = "944203296af76d2e97b4fce3ce90f8ef";
        const amapPlugin = new amap.AMapWX({
            key: GDkey
        })
        uni.showLoading({
            title: "获取信息中"
        });
        const addressName = null; //用户地址 
        return new Promise((resolve, reject) => {
            amapPlugin.getRegeo({
                success: data => {
                    // 来个规范的定位对象的状态
                    var district = data[0].regeocodeData.addressComponent.district;
                    var townStr = data[0].regeocodeData.addressComponent.township;
                    console.log('typeof(district):' + typeof (district));
                    if (typeof (district) != 'string') {
                        district = townStr;
                        townStr = "";
                    }
                    const LocationObj = {
                        province: data[0].regeocodeData.addressComponent.province,
                        cityStr: data[0].regeocodeData.addressComponent.city,
                        areaStr: district,
                        townStr: townStr,
                        deatilAddr: data[0].name,
                        lon: data[0].longitude,
                        lat: data[0].latitude
                    };
                    resolve(LocationObj)
                    uni.hideLoading();
                },
                fail: err => {
                    reject(err);
                    uni.hideLoading();
                }
            })
        })
    },
    /**
     * @function  function
     * @delay time
     * eg
     * debounce(function (e) {
         ...
        }, 1000)
    */
    debounce (func, delay) {
        let timeout
        return function () {
            clearTimeout(timeout) // 如果持续触发，那么就清除定时器，定时器的回调就不会执行。
            timeout = setTimeout(() => {
                func.apply(this, arguments)
            }, delay)
        }
    },
    /* #ifdef MP-WEIXIN */
    onlyMp (fn) {
        fn()
    },
    /* #endif */
    /**
                    * StorageSync 的增删改查
                    * @param {*} key 
                    * @param {*} sync 
                    * @returns 
                    */
    getCache (key, sync = true) {
        try {
            if (sync) {
                return uni.getStorageSync(key);
            } else {
                let data = '';
                uni.getStorage({
                    key: key,
                    success: function (res) {
                        data = res.data;
                    }
                });
                return data;
            }
        } catch (e) {
            return false;
        }
    },
    setCache (key, value, sync = true) {
        try {
            if (sync) {
                return uni.setStorageSync(key, value);
            } else {
                uni.setStorage({
                    key: key,
                    data: value
                });
            }
        } catch (e) {

        }
    },
    delCache (key, sync = true) {
        try {
            if (sync) {
                return uni.removeStorageSync(key);
            } else {
                uni.removeStorage({
                    key: key
                });
            }
        } catch (e) {
            return false;
        }
    },
    clearCache (sync = true) {
        try {
            if (sync) {
                return uni.clearStorageSync();
            } else {
                uni.clearStorage();
            }
        } catch (e) {
            return false;
        }
    }

}

export default utils